package com.droid.mvvmlib.databinding;

import android.util.SparseIntArray;
import android.view.ViewGroup;

import com.droid.mvvmlib.widget.base.BaseCustomerView;
import com.droid.mvvmlib.widget.base.BaseMultiCustomerViewData;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;

/**
 * @author zhyuc
 * @since 2019/10/6 22:40
 */
public abstract class BaseMultiItemBindingQuickAdapter<T extends BaseMultiCustomerViewData,
        K extends BaseBindingViewHolder> extends BaseBindingQuickAdapter<BaseCustomerView, T, K> {

    private static final String TAG = "BaseMultiItemBindingQuickAdapter";

    /**
     * layouts indexed with their types
     */
    private SparseIntArray layouts;

    private static final int DEFAULT_VIEW_TYPE = -0xff;
    public static final int TYPE_NOT_FOUND = -404;


    public BaseMultiItemBindingQuickAdapter() {
        super();
    }

    @NonNull
    @Override
    public BaseCustomerView getView(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    protected int getDefItemViewType(int position) {
        T item = mData.get(position);
        if (item != null) {
            return item.getItemType();
        }
        return DEFAULT_VIEW_TYPE;
    }

    protected void setDefaultViewTypeLayout(@LayoutRes int layoutResId) {
        addItemType(DEFAULT_VIEW_TYPE, layoutResId);
    }

    @Override
    protected K onCreateDefViewHolder(ViewGroup parent, int viewType){
        return super.onCreateDefViewHolder(parent, viewType);
    }

    private int getLayoutId(int viewType) {
        return layouts.get(viewType, TYPE_NOT_FOUND);
    }

    protected void addItemType(int type, @LayoutRes int layoutResId) {
        if (layouts == null) {
            layouts = new SparseIntArray();
        }
        layouts.put(type, layoutResId);
    }
}
